package com.linsen.core.excel.convert;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.linsen.core.common.enums.YnType;

/** 是否 */
public class YnConverter implements Converter<Integer> {
    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        for (YnType ynType : YnType.values()) {
            if (ObjectUtil.equal(ynType.getDesc(),cellData.getStringValue())){
                return ynType.getValue();
            }
        }
        return null;
    }

    @Override
    public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        for (YnType ynType : YnType.values()) {
            if (ObjectUtil.equal(ynType.getValue(),value)){
                return new WriteCellData(ynType.getDesc());
            }
        }
        return null;
    }
}
